package com.xhh.nphm.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.service.*;
import com.xhh.nphm.system.service.impl.MwfJudgeServiceImpl;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/tVstRec")
public class TVstRectelController extends BaseController {
    private String prefix = "system/tVstRec";

    @Autowired
    private IWorkFormBaseService workFormBaseService;
    @Autowired
    private ITelGetDealService telGetDealService;
    @Autowired
    private ITwfJudgeService twfJudgeService;
    @Autowired
    private ITworkFormLogService tworkFormLogService;
    @Autowired
    private IReplyContentService replyContentService;

    /**
     * 待回访电话工单
     */
    @GetMapping("/tel/backWaitList")
    public ModelAndView backWaitList() {
        return new ModelAndView(prefix + "/tel/backWaitList");
    }

    /**
     * 待回访电话工单列表数据
     *
     * @param tworkFormBaseEty
     * @return
     */
    @PostMapping("/tel/backWaitListJson")
    public TableDataInfo backWaitListJson(TworkFormBaseEty tworkFormBaseEty) {
        startPage();
        List<Map<String, Object>> list = workFormBaseService.backWaitListJson(tworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 待回访电话工单 回访页面
     *
     * @param
     * @param map
     * @return
     */
    @GetMapping("/tel/backVisitEdit/{PkId}")
    public ModelAndView backVisitEdit(@PathVariable Integer PkId, ModelMap map) {
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(PkId);
        map.put("tworkFormBaseEty", tworkFormBaseEty);
        TwfJudgeEty twfJudgeEty = twfJudgeService.getById(PkId);
        QueryWrapper<ReplyContent> wrapper = new QueryWrapper<>();
        wrapper.eq("wfId", PkId);
        List<ReplyContent> list = replyContentService.list(wrapper);
        map.put("replyContent", list);
        if (twfJudgeEty != null) {
            map.put("twfJudgeEty", twfJudgeEty);
        }
        return new ModelAndView(prefix + "/tel/backVisitEdit");
    }

    /**
     * 待回访电话工单 回访数据
     */
    @PostMapping("tel/backVisitEditJson")
    @RequestBody
    public AjaxResult backVisitEditJson(TwfJudgeEty twfJudgeEty) {
        String desc = "";
        SysUser user = ShiroUtils.getSysUser();
        UpdateWrapper<TworkFormBaseEty> updateWrapper = new UpdateWrapper<>();
        if (twfJudgeEty.getVstLostC().equals("10")) {
            if (twfJudgeEty.getJudgeC().equals("1")) {
                desc = "评价：不满意";
                updateWrapper.set("wsUFlag", 1)
                        .set("CrtStepID", 905)
                        .set("CrtStepName", "回访不满意")
                        .set("CrtStepTime", new Date())
                        .set("VisitFlag", 21)
                        .set("RecordFlag", 20)
                        .set("RecordTime", new Date())
                        .set("RecUserNo", user.getUserNo())
                        .set("RecUserName", user.getUserName())
                        .eq("PkId", twfJudgeEty.getWfId());
            } else {
                desc = "评价：满意";
                updateWrapper.set("wsUFlag", 1)
                        .set("CrtStepID", 950)
                        .set("CrtStepName", "归档(评价满意)")
                        .set("CrtStepTime", new Date())
                        .set("VisitFlag", 22)
                        .set("RecordFlag", 22)
                        .set("RecordTime", new Date())
                        .set("RecUserNo", user.getUserNo())
                        .set("RecUserName", user.getUserName())
                        .eq("PkId", twfJudgeEty.getWfId());
            }
        } else {
            desc = twfJudgeEty.getVstLostN();
            updateWrapper.set("wsUFlag", 1)
                    .set("CrtStepID", 901)
                    .set("CrtStepName", "回访失败")
                    .set("CrtStepTime", new Date())
                    .set("VisitFlag", 25)
                    .eq("PkId", twfJudgeEty.getWfId());
        }
        boolean flag = workFormBaseService.update(null, updateWrapper);
        twfJudgeService.save(twfJudgeEty);
        if (flag) {
            TworkFormLogEty etyLog = new TworkFormLogEty();
            etyLog.setWfId(twfJudgeEty.getWfId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpBtnId(-1);
            etyLog.setOpAction("录入回访");
            etyLog.setWsDesc("录入回访结果");
            etyLog.setOpDesc("录入回访结果（" + desc + "）");
            etyLog.setWsOpen(0);
            etyLog.setEffectFlag(1);
            etyLog.setLogTime(new Date());
            tworkFormLogService.save(etyLog);
        }
        return toAjax(flag);
    }

    /**
     * 已回访电话工单 列表页面
     *
     * @param
     * @return
     */
    @GetMapping("/tel/backOverList")
    public ModelAndView backOverList() {
        return new ModelAndView(prefix + "/tel/backOverList");
    }

    /**
     * 已回访电话工单列表数据
     *
     * @return
     */
    @PostMapping("/tel/backOverListJson")
    public TableDataInfo backOverListJson(TworkFormBaseEty tworkFormBaseEty) {
        startPage();
        List<TworkFormBaseEty> list = workFormBaseService.backOverListJson(tworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 进入处理已回访电话工单页面
     */
    @GetMapping("/tel/backRsltDeal/{pkId}")
    public ModelAndView backRsltDeal(@PathVariable("pkId") Integer pkId, ModelMap map) {
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(pkId);
        if (tworkFormBaseEty != null) {
            map.put("workFormBase", tworkFormBaseEty);
        }
        return new ModelAndView(prefix + "/tel/backRsltDeal");
    }

}
